//
//  VideoChapter.m
//  WowDara
//
//  Created by Siroj Chongkolwanont on 5/4/56 BE.
//  Copyright (c) 2556 Siroj Chongkolwanont. All rights reserved.
//

#import "VideoChapter.h"
#import "Chapter.h"

@interface VideoChapter ()

@end

@implementation VideoChapter
@synthesize video;
@synthesize VideoLabel;
@synthesize VideoImage;
@synthesize ChapterTable;


NSMutableArray *videoChapter;


id jsonObjectFromUrlString(NSString *urlString);

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)load_chapter
{
    videoChapter = [[NSMutableArray alloc] init];
    
    NSString *chapter_str = [NSString stringWithFormat:@"http://119.59.127.95/~wowdara/android/getChapter.php?id=%@", video.id];
    
    NSDictionary *publicChapter =  jsonObjectFromUrlString(chapter_str);
    
    
    for(NSString *item in [publicChapter allKeys]) {
        
        if([item isEqual: @"chapter"] )
        {
            NSArray *allChapter = [publicChapter valueForKey:item];
            
            for(NSDictionary *chapter in allChapter )
            {
                Chapter* video_obj = [[Chapter alloc] init];
                video_obj.name    = [chapter valueForKey:@"name"];
                video_obj.link    = [chapter valueForKey:@"link"];

                [videoChapter addObject:video_obj];
            }
            
        }
    }
}

- (void)viewDidLoad
{
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"toolbar_logo.png"]];
    UIButton *button1 = [[UIButton alloc] init];
    button1.frame= CGRectMake(0,0,43,32);
    [button1 setBackgroundImage:[UIImage imageNamed: @"nav_back.png"] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
    
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    VideoLabel.text = video.name;
    
    NSString *url_picture = @"http://119.59.127.95/~wowdara/";
    
    url_picture = [url_picture stringByAppendingString:video.images_url];
    
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url_picture]];
    
    [VideoImage setImage:[UIImage imageWithData:imageData]];
    
    [self load_chapter];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {
    NSInteger row = indexPath.row;
    
    Chapter* chapter = videoChapter[row];
    
    
    NSString *chapter_str = [NSString stringWithFormat:@"http://www.youtube.com/watch?v=%@", chapter.link];
    
    NSURL *url=[NSURL URLWithString:chapter_str];
                 [[UIApplication sharedApplication] openURL:url];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger row = [videoChapter count];
    
    return row;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ChapterCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    NSInteger row = indexPath.row;
    
    //News* hilight = hilightArray[row];
    Chapter* feed = videoChapter[row];
    
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    UILabel *recipeNameLabel = (UILabel *)[cell viewWithTag:101];
    recipeNameLabel.text = feed.name;
    
    
    //cell.textLabel.text = hilight.name;
    
    return cell;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)backAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
